package com.isorf.controller;

import com.github.pagehelper.PageInfo;
import com.isorf.pojo.User;
import com.isorf.service.UserService;
import com.isorf.util.JwtUtil;
import com.isorf.util.Md5Util;
import com.isorf.util.ResponseResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user) {

        //查询用户
        User u = userService.findByUserName(user.getAccount());
        if (u == null) {
            //没有占用
            //注册  在service层对密码进行MD5加密
            boolean result  = userService.register(user);
            return new ResponseResult(ResponseResult.SUCCESS, "注册成功", result);
        } else {
            //占用
            return ResponseResult.failure(ResponseResult.FAILED,"用户名已被占用");
        }
    }
    @PostMapping("/login")
    public ResponseResult<String> login(String account, String password, HttpServletRequest request) {
        //根据用户名查询用户
        User loginUser = userService.findByUserName(account);
        //判断该用户是否存在
        if (loginUser == null) {
            return ResponseResult.failure(ResponseResult.FAILED, "用户名错误");
        }

        //判断密码是否正确  loginUser对象中的password是密文
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("account", loginUser.getAccount());
            String token = JwtUtil.getToken(claims);

            //将用户信息存储到Session中
            HttpSession session = request.getSession();
            session.setAttribute("user", loginUser);
            session.setAttribute("token", token);
            //设置session过期时间（可选，默认是30分钟）
            session.setMaxInactiveInterval(60 * 60); // 1小时

            return ResponseResult.success(ResponseResult.SUCCESS, "登录成功", token);
        }
        return ResponseResult.failure(ResponseResult.FAILED, "密码错误");
    }

    @PostMapping("/userList")  // 查询用户列表
    public ResponseResult<List<User>> userList() {
        List<User> userList = userService.userList();
        if (userList == null||userList.size()==0) {
            return ResponseResult.failure(ResponseResult.FAILED, "查询用户列表失败");
        }else {
            return ResponseResult.success(userList);
        }

    }
    //根据用户id修改用户的禁用状态
    @PostMapping("/updateDisable")
    public ResponseResult<Boolean> updateDisable(int id) {
        int result = userService.updateDisable(id);
        if(result>0){
            return ResponseResult.success(ResponseResult.SUCCESS,"修改成功", true);
        }else {
            return ResponseResult.failure(ResponseResult.FAILED,"修改失败");
        }
    }


    //分页查询
    @PostMapping("/pageList")
    //pageNum 是第几页  pageSize 是每页显示多少条数据
    public ResponseResult<PageInfo<User>> pageList(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize){
        PageInfo<User> pageInfo = userService.pageList(pageNum, pageSize);
        if(pageInfo == null || pageInfo.getList().size()==0){
            return ResponseResult.failure(ResponseResult.FAILED,"查询失败");
        }else {
            return ResponseResult.success(ResponseResult.SUCCESS,"查询成功", pageInfo);
        }
    }
}
